﻿using System;
using WF_AppFuel.Validation.Attributes;
using WF_AppFuel.Validation.Base;

namespace WF_AppFuel.Validation.Engines
{
    /// <summary>
    /// This engine is used for validating fields that must have
    /// a value less than a certain check value.
    /// </summary>
    /// <author>Dennis Q. Pitallano</author>
    /// <author>Arjay C. Nacion</author>
    public sealed class IsLessThanValidationEngine : BaseValidationEngine
    {
        public override bool Run()
        {
            IsLessThan attrib = (IsLessThan)ValidationAttribute;

            if (Value == null)
                return false;

            bool isByte = Value is byte;
            bool isShort = Value is short;
            bool isInt = Value is int;
            bool isLong = Value is long;
            bool isFloat = Value is float;
            bool isDouble = Value is double;
            bool isDecimal = Value is decimal;
            bool isString = Value is string;
            bool isDateTime = Value is DateTime;

            //Default comparison method (As Double)
            if (isByte || isShort || isInt || isLong || isFloat || isDouble)
            {
                double checkValue = Convert.ToDouble(attrib.CheckValue);
                double doubleValue = Convert.ToDouble(Value);

                if (doubleValue >= checkValue)
                    return false;
            }else if (isDecimal)
            {
                decimal checkValue = Convert.ToDecimal(attrib.CheckValue);
                decimal decimalValue = Convert.ToDecimal(Value);

                if (decimalValue >= checkValue)
                    return false;
            }else if (isDateTime)
            {
                DateTime checkValue = Convert.ToDateTime(attrib.CheckValue);
                DateTime dateTimeValue = Convert.ToDateTime(Value);
                int dateTimeComparison = DateTime.Compare(dateTimeValue, checkValue);

                if (dateTimeComparison >= 0)
                    return false;

            }else if (isString)
            {
                string checkValue = attrib.CheckValue.ToString();
                string stringValue = Value.ToString();
                int stringComparison = String.Compare(stringValue, checkValue,
                    StringComparison.OrdinalIgnoreCase);

                if (stringComparison >= 0)
                    return false;
            }
            return true;
        }
    }
}
